<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleResult()" v-auth="'btn:result:zhenggai'">
          <Icon icon="ant-design:edit-outlined" /> {{ t('整改结果') }}
        </a-button>
        <a-button type="primary" @click="handleAccept()" v-auth="'btn:check:zhenggai'">
          <Icon icon="fluent:add-12-filled" /> {{ t('整改验收') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.name }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectno }}
        </a>
        
      </template>
      <template #rectificationdate="{ record }">
        {{ formatDate(record.rectificationdate) }}
      </template>
      <template #uploadFile="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          v-if="fileShow"
          :bizKey="record.id"
          :bizType="'pmRectification_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
      <template #uploadFileZg="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'rectList_file'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <ResultForm @register="registerResultDrawer" @success="handleSuccess" />
    <AcceptForm @register="registerAcceptDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjZgPmRectificationList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted,ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmRectificationDelete, pmRectificationListData } from '/@/api/proj/zg/pmRectification';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import ResultForm from './resultForm.vue';
  import AcceptForm from './acceptForm.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { BasicUpload } from '/@/components/Upload';
  const userStore = useUserStore();

  const { t } = useI18n('proj.zg.pmRectification');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('项目整改管理'),
  };
  let fileShow = ref(true)

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工程编号'),
        field: 'projectno',
        component: 'Input',
      },
      {
        label: t('工程名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createtime',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('整改措施'),
        field: 'rectificationmeasures',
        component: 'Input',
      },
      {
        label: t('整改日期'),
        field: 'rectificationdate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('验收时间'),
        field: 'checkdate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('发起人'),
        field: 'acceptor',
        component: 'Input',
      },
      {
        label: t('整改期限'),
        field: 'endtime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
    ],
    fieldMapToTime: [
      ['createtime', ['createtime_gte', 'createtime_lte']],
      ['rectificationdate', ['rectificationdate_gte', 'rectificationdate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工程名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 280,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工程编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'projectno',
      fixed:'left'
    },
    {
      title: t('创建时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('整改图片'),
      dataIndex: 'billno',
      key: 'a.billno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'uploadFileZg',
    },
    {
      title: t('整改期限'),
      dataIndex: 'endtime',
      key: 'a.endtime',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('整改内容'),
      dataIndex: 'rectificationcontent',
      key: 'a.rectificationcontent',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('整改措施'),
      dataIndex: 'rectificationmeasures',
      key: 'a.rectificationmeasures',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('整改结果'),
      dataIndex: 'rectificationresult',
      key: 'a.rectificationresult',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('完成图片'),
      dataIndex: 'rectificationpic',
      key: 'a.rectificationpic',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'uploadFile',
    },
    {
      title: t('整改日期'),
      dataIndex: 'rectificationdate',
      key: 'a.rectificationdate',
      sorter: true,
      width: 150,
      align: 'center',
      slot: 'rectificationdate',
    },
    {
      title: t('验收结果'),
      dataIndex: 'checkresult',
      key: 'a.checkresult',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'zf_check_result',
      fixed: 'right',
    },
    {
      title: t('验收意见'),
      dataIndex: 'checkopinion',
      key: 'a.checkopinion',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('验收时间'),
      dataIndex: 'checkdate',
      key: 'a.checkdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('发起人'),
      dataIndex: 'acceptor',
      key: 'a.acceptor',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑项目整改'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'proj:zg:pmRectification:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除项目整改'),
        popConfirm: {
          title: t('是否确认删除项目整改'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'proj:zg:pmRectification:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerResultDrawer, { openDrawer: openResultDrawer }] = useDrawer();
  const [registerAcceptDrawer, { openDrawer: openAcceptDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: pmRectificationListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: true,
    rowSelection: {
      type: 'radio',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  // 时间格式化日期，只保留年月日
  const formatDate = (date: string) => {
    if (!date) return '';
    const dateObj = new Date(date);
    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1;
    const day = dateObj.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  };

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 整改结果
  function handleResult() {
    if (getSelectRowKeys().length) {
      openResultDrawer(true, { id: getSelectRowKeys()[0] });
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 整改验收
  function handleAccept() {
    if (getSelectRowKeys().length) {
      openAcceptDrawer(true, { id: getSelectRowKeys()[0] });
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  async function handleDelete(record: Recordable) {
    const res = await pmRectificationDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    fileShow.value = false
    reload();
    setTimeout(()=>{
      fileShow.value = true
    },500)
  }

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
  }
</script>
